package spring.cloud.tasks.client;

import lombok.extern.slf4j.Slf4j;
import spring.cloud.tasks.client.batch_task_processor.BatchTaskThreadManager;
import spring.cloud.tasks.client.item_task_processor.ItemTaskThreadManager;
import spring.cloud.tasks.common.zookeeper.ZooKeeperBatchTaskDataManager;
import spring.cloud.tasks.common.zookeeper.ZooKeeperItemTaskDataManager;

import java.util.Properties;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


@Slf4j
public class Client {
    private static volatile boolean inited = false;
    private static ItemTaskWorker itemTaskWorker;
    private static BatchTaskWorker batchTaskWorker;


    public synchronized static boolean init() throws Exception {
        if (inited) {
            return true;
        }
        if (itemTaskWorker == null) {
            itemTaskWorker = new ItemTaskWorker();
            itemTaskWorker.executeTasks = false;
            Properties properties = new Properties();
            itemTaskWorker.init(properties);
            itemTaskWorker.executeTasks = true;
        }
        if (batchTaskWorker == null) {
            batchTaskWorker = new BatchTaskWorker();
            batchTaskWorker.executeTasks = false;
            Properties properties = new Properties();
            batchTaskWorker.init(properties);
            batchTaskWorker.executeTasks = true;
        }
        threadMaintain();
        inited = true;
        return true;
    }


    public static void setWorker(Worker worker) {
        if (worker == null) {
            return;
        }
        if (worker instanceof ItemTaskWorker) {
            setItemTaskWorker((ItemTaskWorker) worker);
        } else if (worker instanceof BatchTaskWorker) {
            setBatchTaskWorker((BatchTaskWorker) worker);
        } else {
            throw new IllegalStateException();
        }
    }

    public static void setItemTaskWorker(ItemTaskWorker itemTaskWorker) {
        Client.itemTaskWorker = itemTaskWorker;
    }

    public static void setBatchTaskWorker(BatchTaskWorker batchTaskWorker) {
        Client.batchTaskWorker = batchTaskWorker;
    }

    public synchronized static ZooKeeperItemTaskDataManager getZooKeeperItemTaskDataManager() throws Exception {
        if (!inited) {
            init();
        }
        return itemTaskWorker.getZooKeeperItemTaskDataManager();
    }

    public synchronized static ZooKeeperBatchTaskDataManager getZooKeeperBatchTaskDataManager() throws Exception {
        if (!inited) {
            init();
        }
        return batchTaskWorker.getZooKeeperBatchTaskDataManager();
    }

    //线程维护线程
    public static void threadMaintain() {
        final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                ItemTaskWorker itemTaskWorker = Client.itemTaskWorker;
                if (itemTaskWorker != null) {
                    try {
                        ItemTaskThreadManager.threadMaintain(itemTaskWorker);
                    } catch (Throwable throwable) {
                        log.error("", throwable);
                    }
                }
                BatchTaskWorker batchTaskWorker = Client.batchTaskWorker;
                if (itemTaskWorker != null) {
                    try {
                        BatchTaskThreadManager.threadMaintain(batchTaskWorker);
                    } catch (Throwable throwable) {
                        log.error("", throwable);
                    }

                }
            }
        }, 10, 5, TimeUnit.SECONDS);
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                scheduledExecutorService.shutdown();
            }
        }));
    }
}
